home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / parsingcommandline / example2.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  11KB  |  268 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Parsing Command Line        Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-06                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to parse the command line    */
  21. /* with several arguments. This example handles two types of  */
  22. /* command templates. First it can collect one or more        */
  23. /* words which will be used as file names. This demonstrates  */
  24. /* the "/M" (Multiple argument) option. Secondly the example  */
  25. /* accepts a special argument used as a switch. This          */
  26. /* demonstrates the "/S" ("Switch") option. The special       */
  27. /* argument is "Filter", but can also be abbreviated as "F".  */
  28.  
  29.  
  30.  
  31. /* Include the dos library definitions: */
  32. #include <dos/dos.h>
  33.  
  34. /* Include information about the argument parsing routine: */
  35. #include <dos/rdargs.h>
  36.  
  37. /* Now we include the necessary function prototype files:         */
  38. #include <clib/dos_protos.h>       /* General dos functions...    */
  39. #include <clib/exec_protos.h>      /* System functions...         */
  40. #include <stdio.h>                 /* Std functions [printf()...] */
  41. #include <stdlib.h>                /* Std functions [exit()...]   */
  42.  
  43.  
  44.  
  45. /* Here is our command line template. This program handles two     */
  46. /* types of command templates:                                     */
  47. /*                                                                 */
  48. /* 1. "SoundFiles/A/M" The program accepts one or more arguments   */
  49. /*                     which will be used as file names. The "/A"  */
  50. /*                     option tells the ReadArgs() function that   */
  51. /*                     at least one file name must be given. The   */
  52. /*                     "/M" option tells the ReadArgs() function   */
  53. /*                     that this template should accept several    */
  54. /*                     arguments if necessary. (All arguments      */
  55. /*                     which can not be placed anywhere else will  */
  56. /*                     go here. Please note that only one "/M"     */
  57. /*                     option can be used in the command line      */
  58. /*                     template.)                                  */
  59. /*                                                                 */
  60. /* 2. "F=Filter/S"     The user has an option of adding the        */
  61. /*                     argument "Filter". The "/S" option tells    */
  62. /*                     the ReadArgs() function that this argument  */
  63. /*                     should be treated as a switch. If the       */
  64. /*                     argument is set the switch will be turned   */
  65. /*                     "on", else it will be "off". The "F="       */
  66. /*                     string means that the user also can use the */
  67. /*                     abbreviation "F" in stead of writing the    */
  68. /*                     whole argument "Filter".                    */
  69. /*                                                                 */
  70. /* (Note the comma [,] between the command templates and that      */
  71. /* there are no spaces [ ].)                                       */
  72. #define MY_COMMAND_LINE_TEMPLATE "SoundFiles/A/M,F=Filter/S"
  73.  
  74. /* Here are some valid command lines:                              */
  75. /*   Example2 Bird.snd                                             */
  76. /*   Example2 Bird.snd River.snd                                   */
  77. /*   Example2 Bird.snd River.snd Sea.snd                           */
  78. /*   Example2 Bird.snd Filter                                      */
  79. /*   Example2 Bird.snd River.snd F                                 */
  80. /*   Example2 Bird.snd Filter River.snd Sea.snd                    */
  81. /*                                                                 */
  82. /* Here are some incorrect command lines:                          */
  83. /*   Example2                      One file name is required!      */
  84. /*   Example2 Filter                          - " -                */
  85.  
  86.  
  87.  
  88. /* Two command templates are used: */
  89. #define NUMBER_COMMAND_TEMPLATES  2
  90.  
  91. /* The command template numbers: (Where the result of each */
  92. /* command template can be found in the "arg_array".)      */
  93. #define SOUNDFILES_TEMPLATE  0
  94. #define FILTER_TEMPLATE      1 
  95.  
  96.  
  97.  
  98. /* Set name and version number: */
  99. UBYTE *version = "$VER: AmigaDOS/ParsingCommandLine/Example2 1.0";
  100.  
  101.  
  102.  
  103. /* Declare an external global library pointer to the Dos library: */
  104. extern struct DosLibrary *DOSBase;
  105.  
  106.  
  107.  
  108. /* Declared our own function(s): */
  109.  
  110. /* Our main function: */
  111. int main( int argc, char *argv[] );
  112.  
  113.  
  114.  
  115. /* Main function: */
  116.  
  117. int main( int argc, char *argv[] )
  118. {
  119.   /* Simple loop variable: */
  120.   int loop;
  121.  
  122.   /* Store the pointer to the array of string pointers here: */
  123.   UBYTE **string_array;
  124.  
  125.   /* Pointer to a RDArgs structure which will automatically */
  126.   /* be created for us when we use the RDArgs() function:   */
  127.   struct RDArgs *my_rdargs;
  128.  
  129.   /* The ReadArgs() function needs an arrya of LONGs where */
  130.   /* the result of the command parsing will be placed. One */
  131.   /* LONG variable is needed for every command template.   */
  132.   LONG arg_array[ NUMBER_COMMAND_TEMPLATES ];
  133.  
  134.   /* Note! This "arg_array" must be cleared (all values set to */
  135.   /* zero) before we may use it with the ReadArgs() function.  */
  136.   /* If we declare this structure outside the main function    */
  137.   /* all values will automatically be cleared by C, but if we, */
  138.   /* as in this example, declare the array inside a function   */
  139.   /* we have to clear it manually. (If we do not clear it we   */
  140.   /* can not examine the array and see if a field is set or    */
  141.   /* not.)                                                     */
  142.  
  143.  
  144.  
  145.   /* The built in command parsing routine was first  */
  146.     /* introduced in Release 2. V36 of the dos library */
  147.     /* was however rather "buggy", and you should only */
  148.     /* use V37 or higher:                              */
  149.   if( DOSBase->dl_lib.lib_Version < 37 )
  150.   {
  151.     /* Too old dos library! */
  152.     printf( "This program needs Dos Library V37 or higher!\n" );
  153.    
  154.     /* Exit with an error code: */ 
  155.     exit( 20 );
  156.   }
  157.  
  158.  
  159.  
  160.   /* We will now clear the "arg_array" (set all values to zero): */
  161.   for( loop = 0; loop < NUMBER_COMMAND_TEMPLATES; loop++ )
  162.     arg_array[ loop ] = 0;
  163.  
  164.  
  165.  
  166.   /* Parse the command line: (ReadArgs() will read the command   */
  167.   /* line and with the help of the command line template set     */
  168.   /* the corresponding values in the "arg_array" which is used   */
  169.   /* to store the result of the command parsing. The function    */
  170.   /* will return a pointer to a RDArgs structure which has       */
  171.   /* automatically been created for us, since we did not create  */
  172.   /* one ourself. This structure must be removed with help of    */
  173.   /* the FreeArgs() function before your program may terminate.) */
  174.   my_rdargs = 
  175.     ReadArgs( MY_COMMAND_LINE_TEMPLATE,
  176.               arg_array,
  177.               NULL
  178.             );
  179.  
  180.   /* Have AmigaDOS successfully parsed our command line? */
  181.   if( !my_rdargs )
  182.   {
  183.     /* The command line could not be parsed! The user probably */
  184.     /* forgot to enter an argument which is required.          */
  185.     printf( "Could not parse the command line!\n" );
  186.  
  187.     /* Life isn't fair... */
  188.     exit( 21 );
  189.   }
  190.  
  191.   /* The comand line has successfully been parsed! */
  192.   /* We can now examine the "arg_array":           */
  193.  
  194.  
  195.  
  196.   /* Print template 1, the file name argument. Since the user may */
  197.   /* enter several file names (the "/M" option is set) the value  */
  198.   /* in the "arg_array" will not be a pointer to a string.        */
  199.   /* Instead, the value in the "arg_array" will be a pointer to   */
  200.   /* another array of strings where the file names are stored.    */
  201.   /* Please note that this will only happen if you have set the   */
  202.   /* "/M" option.                                                 */
  203.  
  204.   /* Are there any file names (there must be at least one  */
  205.   /* in this example, the "/A" option is se, but we better */
  206.   /* check it anyway...)                                   */
  207.   if( arg_array[ SOUNDFILES_TEMPLATE ] )
  208.   {
  209.     /* Store the pointer to the array of stirng pointers: */
  210.     /* (I agree that double pointers look horrible...)    */ 
  211.     string_array = (UBYTE **) arg_array[ SOUNDFILES_TEMPLATE ];
  212.   
  213.     /* What we have to do now is to examine all strings with help of */
  214.     /* a simple while loop. The last string in the array will be set */
  215.     /* to NULL so we know were the list ends.                        */
  216.     
  217.     /* Start with the first string: */
  218.     loop = 0;
  219.  
  220.     /* Print all file names: */
  221.     while( string_array[ loop ] )
  222.     {
  223.       /* Print the file name: */
  224.       printf( "File name: %s\n", string_array[ loop ] );
  225.  
  226.       /* Increase the counter: */
  227.       loop++;
  228.     }
  229.     
  230.     /* All file names have now been printed! */
  231.   }
  232.   
  233.   
  234.   
  235.   /* Print template 2, the filter switch. Since this is a switch  */
  236.   /* argument it can either be on or off. If the user has entered */
  237.   /* the argument "Filter" or the abbreviation "F" the second     */
  238.   /* field in the "arg_array" will contain a non-zero number,     */
  239.   /* else (the user has not entered the argument "Filter" or "F") */
  240.   /* the second field in the "arg_array" is set to zero.          */
  241.  
  242.   /* Was the argument "Filter" or "F" set? */  
  243.   if( arg_array[ FILTER_TEMPLATE ] )
  244.     printf( "The sound filter was turned on!\n" );
  245.   else
  246.     printf( "No sound filter will be used!\n" );
  247.  
  248.  
  249.  
  250.   /* Before our program terminates we have to free the RDArgs */
  251.   /* structure which was automatically allocated for us:      */
  252.   FreeArgs( my_rdargs );
  253.  
  254.   /* Please note that the arguments that was collected by the */
  255.   /* ReadArgs() function will also be removed when you call   */
  256.   /* FreeArgs. Any pointers in the "result_templates" array   */
  257.   /* which pointed to some data, for example strings, may     */
  258.   /* therefore not be used any more after you have called     */
  259.   /* FreeArgs(). The data (strings) will have been            */
  260.   /* deallocated.                                             */
  261.   
  262.  
  263.  
  264.   /* "And they lived happily ever after..." */
  265.   exit( 0 );
  266. }
  267.  
  268.